#import "SEGTopicTextView.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

/**************************SEGTopicTextView************************/
@implementation SEGTopicTextView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kTopicTextViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.contentBgView];
    [self.contentBgView addSubview:self.contentView];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.contentLeftImageView];
    
    [self addSubview:self.topicTitleLabel];
    
   // //if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGTopicTextViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.contentLabel.textLayout = layout.contentLabelTextLayout;
    self.topicTitleLabel.textLayout = layout.topicTitleLabelTextLayout;

    [self setupUI];
}

//界面布局
-(void)setupUI{
    
    [self.contentBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.width.mas_equalTo(_layout.data.widgetWidth);
        make.height.mas_equalTo(_layout.contentBgViewHeight);
    }];
    
    if (_layout.data.isMoreLines) { //多于三行 上下间距固定
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentBgView);
            make.top.equalTo(self.contentBgView).offset(contentTopPadding);
            make.width.mas_equalTo(_layout.contentLabelWidth + contentLabelLeftPadding);
            make.height.mas_equalTo(_layout.contentViewHeight);
        }];
    }else {//少于三行 居中
        [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentBgView);
            make.centerY.equalTo(self.contentBgView);
            make.width.mas_equalTo(_layout.contentLabelWidth + contentLabelLeftPadding);
            make.height.mas_equalTo(_layout.contentViewHeight);
        }];
    }
    
    [self.contentLeftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView);
    }];
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(contentLabelTopPadding);
        make.left.equalTo(self.contentView).offset(contentLabelLeftPadding);
        make.width.mas_equalTo(_layout.contentLabelWidth);
        make.height.mas_equalTo(_layout.contentLabelHeight);
    }];

    self.topicTitleLabel.backgroundColor = kColor_L3;
    self.topicTitleLabel.layer.cornerRadius = 11.5;
    self.topicTitleLabel.layer.masksToBounds = YES;
    [self.topicTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self.contentBgView.mas_bottom).offset(10);
        make.height.mas_equalTo(_layout.topicTitleLabelHeight);
        make.width.mas_equalTo(_layout.data.topicTitleWidth);
    }];

//    if (!self.layout.data.bgColor) {
//        self.layout.data.bgColor = [UIColor topicBgColor];
//    }
//    self.contentLabel.backgroundColor = self.layout.data.bgColor;

    self.contentLabel.textAlignment = NSTextAlignmentLeft;
    self.contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.topicTitleLabel.textAlignment = NSTextAlignmentLeft;
    self.topicTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

//- (void)setTextBgColor:(UIColor *)textBgcolor
//{
//    if (textBgcolor) {
//        self.layout.data.bgColor = textBgcolor;
//        self.contentLabel.backgroundColor = self.layout.data.bgColor;
//    }
//}

#pragma mark - getter
-(SEGLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[SEGLabel alloc]init];
        _contentLabel.userInteractionEnabled = NO;
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentLabel.displaysAsynchronously = YES;
//        _contentLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentLabel.fadeOnHighlight = NO;
        _contentLabel.numberOfLines = 0;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
//        _contentLabel.backgroundColor = kColor_L2;
       // //if (kDebug)_contentLabel.backgroundColor = kSegRandomColor;
        //__weak typeof(self) _self = self;
        //_contentLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //[SEGAlertView showTmepMessageWithMessage:@"_contentLabelClick"];
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocolcontentLabel:)]) {
        //[_self.delegate SEGprotocolcontentLabel:_self.contentLabel];
        //}
        //};
    }
    return _contentLabel;
}

- (UIImageView *)contentLeftImageView
{
    if (!_contentLeftImageView) {
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_yinhao")];
        _contentLeftImageView = [[UIImageView alloc] initWithImage:image];
    }
    return _contentLeftImageView;
}

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
//        _contentView.backgroundColor = kColor_bg;//[UIColor colorWithRed:255.0 / 255.0 green:249.0 / 255.0 blue:234.0 / 255.0 alpha:1];
    }
    return _contentView;
}

- (UIView *)contentBgView
{
    if (!_contentBgView) {
        _contentBgView = [[UIView alloc] init];
        _contentBgView.backgroundColor = kColor_L3;
    }
    return _contentBgView;
}


-(SEGLabel *)topicTitleLabel{
    if (!_topicTitleLabel) {
        _topicTitleLabel = [[SEGLabel alloc]init];
        _topicTitleLabel.userInteractionEnabled = YES;
        _topicTitleLabel.textAlignment = NSTextAlignmentLeft;
        _topicTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _topicTitleLabel.displaysAsynchronously = YES;
//        _topicTitleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _topicTitleLabel.fadeOnHighlight = NO;
        _topicTitleLabel.numberOfLines = 1;
        _topicTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _topicTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _topicTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        ////if (kDebug)_topicTitleLabel.backgroundColor = kSegRandomColor;
        
        
        _topicTitleLabel.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(topicTextTopicTitleLabel:)]) {
                [_self.delegate topicTextTopicTitleLabel:_topicTitleLabel];
            }
        }];
        [_topicTitleLabel addGestureRecognizer:tap];
        
    }
    return _topicTitleLabel;
}

@end
